package compile.processor;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import java.util.Set;

/**
 * 检查命名规范的编译插件
 * 使用方式：
 *   javac -classpath /Users/guopanbo/IdeaProjects/understanding-jvm/out/production/understanding-jvm
 *      -processor compile.processor.NameCheckProcessor
 *      compile/processor/BAD_NAMED_CODE.java
 */
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class NameCheckProcessor extends AbstractProcessor {

    private NameChecker nameChecker;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        nameChecker = new NameChecker(processingEnv.getMessager());
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        if(!roundEnv.processingOver()) {
            roundEnv.getRootElements().forEach(nameChecker::check);
        }
        return false;
    }
}
